package com.example.himalayaproject.find.monthlyfocus.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.find.monthlyfocus.WebViewActivity;
import com.example.himalayaproject.find.monthlyfocus.entity.Month_List;
import com.squareup.picasso.Picasso;

/**榜单页中又嵌套一个recyclerView的适配器,获取每个item中的详细内容

 */
public class RecycleViewSceondAdapter extends RecyclerView.Adapter implements View.OnClickListener {
    private Context context;
    private Month_List month_list;//榜单页的数据源
    private final int TYPE_EMPTY=1;
    private final int TYPE_NORMAL=2;


    public RecycleViewSceondAdapter(Context context, Month_List month_list) {
        this.context = context;
        this.month_list = month_list;

    }


    @Override
    public int getItemViewType(int position) {
        if (position%2!=0) {
            return TYPE_EMPTY;
        }else{
            return TYPE_NORMAL;
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=null;
        switch (viewType) {
            case TYPE_EMPTY:
                view = LayoutInflater.from(context).inflate(R.layout.between_items_empty, parent, false);
                EmptyViewHolder emptyViewHolder = new EmptyViewHolder(view);
                return emptyViewHolder;
            case  TYPE_NORMAL:
                view = LayoutInflater.from(context).inflate(R.layout.fragmet_item_peritem, parent, false);
                return new SecondViewHolder(view);
        }

        return null;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SecondViewHolder) {
            int index=position/2;

                Picasso.with(context).load(month_list.getList().get(index).getCoverPath()).into( ((SecondViewHolder) holder).ivPic);

                ((SecondViewHolder) holder).title.setText(month_list.getList().get(index).getTitle());

                ((SecondViewHolder) holder).tvTitleOne.setText("1 "+month_list.getList().get(index).getFirstKResults().get(0).getTitle());
                ((SecondViewHolder) holder).tvTitleTwo.setText("2 "+month_list.getList().get(index).getFirstKResults().get(1).getTitle());
                 ((SecondViewHolder) holder).linearLayout.setOnClickListener(this);

        }

    }
    @Override
    public int getItemCount() {
        return month_list.getCount()*2;
    }

    @Override
    public void onClick(View view) {
        int position=((RecyclerView)view.getParent()).getChildAdapterPosition(view);
        Intent intent=new Intent(context, WebViewActivity.class);
        context.startActivity(intent);
    }


    public class SecondViewHolder extends RecyclerView.ViewHolder{

        private final ImageView ivPic;
        private final TextView title;
        private final TextView tvTitleOne;
        private final TextView tvTitleTwo;
        private LinearLayout linearLayout;

        public SecondViewHolder(View itemView) {
            super(itemView);
            ivPic = ((ImageView) itemView.findViewById(R.id.iv_frg_ranking_item_peritem));
            //各个标题
            title= (TextView) itemView.findViewById(R.id.tv_frg_ranking_item_peritem_title);
            tvTitleOne = ((TextView) itemView.findViewById(R.id.tv_frg_ranking_item_peritem_pertitle_one));
            tvTitleTwo = ((TextView) itemView.findViewById(R.id.tv_frg_ranking_item_peritem_pertitle_two));
            linearLayout= (LinearLayout) itemView.findViewById(R.id.linearLa);
        }
    }

    public class EmptyViewHolder extends RecyclerView.ViewHolder{

        public EmptyViewHolder(View itemView) {
            super(itemView);
        }
    }
}
